package com.ftg.learn.student.service;

import com.ftg.learn.student.entity.Menu;
import com.ftg.learn.student.entity.Student;
import com.ftg.learn.student.mapper.StudentMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author kangjx
 * @createTime 2022/7/29
 */
@Service
public class StudentService {

    @Autowired
    StudentMapper studentMapper;

    public List<Student> show() {
        return studentMapper.showAllStudent();
    }

    public List<Student> findid(String id) {
        return studentMapper.findid(id);
    }

    public List<Student> findid1(String id) {
        return studentMapper.findid1(id);
    }

    public List<Student> findid2(String id) {
        return studentMapper.findid2(id);
    }

    /**
     * 业务功能处理，为空判断 ，主务功能判断，业务上流程处理
     * @param sno
     * @param sname
     * @param ssage
     * @param ssex
     * @return
     */
    public Map<String,Object> save(String sno, String sname, Integer ssage, String ssex) {
        Map<String,Object> map = new HashMap<>(2);
        if(Objects.isNull(sno)){
            map.put("mess","主键不能为空!!!");
            return map;
        }

        // sno是没有可能已经存在了
        Student stu = studentMapper.getOneBySno(sno);
        if(Objects.nonNull(stu)){
            map.put("mess","主键" + sno+"已存在！请更换!!");
            return map;
        }
        int num = studentMapper.save(sno,sname,ssage,ssex);
        map.put("mess","添加已成功!!!");
        map.put("data",num);
        return map;
    }

    public Map<String,Object> savePojo(Student student) {
        Map<String,Object> map = new HashMap<>(2);
        if(Objects.isNull(student.getSno())){
            map.put("mess","主键不能为空!!!");
            return map;
        }

        // sno是没有可能已经存在了
        Student stu = studentMapper.getOneBySno(student.getSno());
        if(Objects.nonNull(stu)){
            map.put("mess","主键" + student.getSno()+"已存在！请更换!!");
            return map;
        }
        int num = studentMapper.savePojo(student);
        map.put("mess","添加已成功!!!");
        map.put("data",num);
        return map;
    }

    /**
     * 用来列出所有学生以及成绩信息
     * @return
     */
    public List<Student> showAllWithSc(){
        return studentMapper.showAllWithSc();
    }

    /**
     * 用来列出所有学生以及成绩信息
     * @return
     */
    public List<Menu> allMenu(){
        return studentMapper.allMenu();
    }

}
